﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ARS.Web.Models;
using ARS.Common.Helper;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;

namespace ARS.Web.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult EmailTemplate()
        {
            ViewBag.ExecPath = Program.appSetting.ExecPath;
            return View();
        }
        public IActionResult EmailTask_Logs()
        {

            return View();
        }
        public IActionResult EmailTask()
        {
            return View();
        }
        public IActionResult MailContacts()
        {
            return View();
        }
        public IActionResult SQlInstance()
        {
            return View();
        }

        /// <summary>
        /// Stmp配置
        /// </summary>
        /// <returns></returns>
        public IActionResult StmpSetting()
        {
            return View();
        }

        public IActionResult Summary()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
        /// <summary>
        /// 文件中转站 功能页面
        /// </summary>
        /// <returns></returns>
        [Route("ars/files")]
        public IActionResult Files()
        {
            var xx = DateTimeHelper.GetLongbyTime(DateTime.Now.AddDays(30));
            var times = Request.Query["t"].ToString();
            //var fileName = Request.Query["f"];
            ViewBag.TimeDesc = "已过期";
            ViewBag.ShowDown = 0;
            if (!string.IsNullOrEmpty(times))
            {

                var tt = DateTimeHelper.GetTimebyLong(long.Parse(times)) - DateTime.Now;
                if (tt.TotalSeconds > 0)
                {
                    ViewBag.ShowDown = 1;
                    ViewBag.TimeDesc = $"{new DateTime(tt.Ticks):dd天 HH小时mm分钟}";
                }
            }

            return View();
        }
        /// <summary>
        /// 下载文件 文件中转站
        /// </summary>
        /// <returns></returns>
        [Route("ars/down")]
        public IActionResult Down()
        {
            var path = Environment.CurrentDirectory;
             
            //var filePath = $"/exportExcel/蒋丹静_资金流_月报QHHMHNI0.xlsx";
            var files = Request.Query["f"].ToString();
            var times = Request.Query["t"].ToString();
            var vn = Request.Query["vn"].ToString();
            var vcode = Request.Query["vcode"].ToString();
            
            var verifiedValue=Common.Service.BuilderServices.MatchCode(times,files, vcode);
            if (vn == verifiedValue)
            {
                #region set contentType
                var filePath = $"{path}/exportExcel/{files}";
                var _a = filePath.Split('.');
                var suffix = _a[_a.Length - 1];
                string contentType;
                switch (suffix)
                {
                    case "xls":
                        contentType = "application/x-xls";
                        break;
                    case "xlsx":
                        contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        break;
                    case "csv":
                        contentType = "text/csv";
                        break;
                    default:
                        contentType = "application/octet-stream";
                        break;

                }
                #endregion

                //contentType
                //return File(filePath, contentType, "aa.xlsx");
                return PhysicalFile(filePath, contentType,files);
            }
            else
            {
                return Content("数据验证失败,请联系大数据组");
            }
            // 
        }
        //public FileResult DownloadFile()
        //{
        //    return File("/Files/File Result.pdf", "text/plain", "File Result.pdf");
        //}

        public IActionResult Test()
        {
            return View();
        }
    }
}
